<extend name="common:commonNewerTemplate" />
<block name="main_content">
<div id="content">
	<div id="content-header">
		<div id="breadcrumb">
			<a class="tip-bottom" href="index.html"
				data-original-title="Go to Home"><i class="icon-home"></i> 首页</a>
			<a href="#">业务员</a> <a class="current" href="#">务员管理</a>
		</div>
	</div>
	<div class="container-fluid">
		<div class="row-fluid">
			<div class="span12">
				<div class="widget-box">
					<div class="widget-title">
						<span class="icon"> <i class="icon-info-sign"></i>
						</span>
						<h5>搜索条件</h5>
					</div>
					<div class="widget-content nopadding">
						<form novalidate="novalidate" id="basic_validate"
							name="basic_validate" action="#" method="post"
							class="form-horizontal">
							<div class="control-group">
								<div class="control-group">
								<label class="control-label">业务员编号：</label>
								<div class="controls">
									<input type="text" id="sale_id" name="sale_id">
									<input type="hidden" id="is_Business" name="is_Business" value="{$is_Business}">
								</div>
							</div>
							</div>
							<div class="form-actions">
								<input id="btn_sale_search" type="button" class="btn btn-success" value="搜索">
								
															
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	
	<div class="row-fluid">
      <div class="span6">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>上级关系列表</h5>
            <span style="float:right;margin:3px;">
				<input id="btn_sale_add_prelevel" type="button"  style="background:red" class="btn btn-success" value="新增上级">		
			</span>
          </div>
          <div class="widget-content nopadding">
            <table id="search_result_prelevel"  class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>业务员编号</th>
                  <th>用户名</th>
                  <th>真实姓名</th>
                  <!-- <th>大区</th> -->
                 <!--  <th>省名称</th>
                  <th>市名称</th>
                  <th>区名称</th> -->
                  <th>手机号码</th>
                  <th>部门</th>
                  <!-- <th>职能</th>
                  <th>负责区域</th> -->
                  <th>邮箱</th>
                <!--   <th>用户地址</th> -->
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
              </tbody>
            </table>
          </div>
      </div>
   
    </div>
    
     <div class="span6">
        <div class="widget-box">
          <div class="widget-title"> <span class="icon"> <i class="icon-th"></i> </span>
            <h5>下级关系列表</h5>
            <span style="float:right;margin:3px;">
				<input id="btn_sale_add_sublevel" type="button"  style="background:red" class="btn btn-success" value="新增下级">
			</span>
          </div>
          <div class="widget-content nopadding">
            <table id="search_result_sublevel"  class="table table-bordered table-striped">
              <thead>
                <tr>
                  <th>业务员编号</th>
                  <th>用户名</th>
                  <th>真实姓名</th>
                 <!--  <th>大区</th> -->
                  <!-- <th>省名称</th>
                  <th>市名称</th>
                  <th>区名称</th> -->
                  <th>手机号码</th>
                  <th>部门</th>
                  <!-- <th>职能</th>
                  <th>负责区域</th> -->
                  <th>邮箱</th>
                <!--   <th>用户地址</th> -->
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
              </tbody>
            </table>
          </div>
      </div>
   
    </div>
  </div>

</div>
</div>
</block>

<block name="js_content">
<script src="/Public/js/layer/layer.js" type="text/javascript"></script>
<script>
SaleRelationPage = {
		'BindEvent': function(){
			$('#btn_sale_search').bind('click', function(){				 
				var condition = $("#basic_validate").serialize();
				 SaleRelationPage.Search();
			});
			

			
			$('#btn_sale_add_prelevel').bind('click', function(){
				
				 
				 SaleRelationPage.SearchSalePrelevel();
			});
			
			
			$('#btn_sale_add_sublevel').bind('click', function(){
				
				 
				 SaleRelationPage.SearchSaleSublevel();
			});
			
			
		
			
			
           
		},
		
	 'delRelationship':function(sale_id){
		 
		
		var condition = "sale_id="+sale_id;
		if(!confirm("确认删除此记录?")){
			return;
		}
		   $.ajax({
              type: 'post',
              url: '/AjaxService/center/sale/delsalerelationship',
              data: condition,
              dataType: 'json',
              async: false,
              success: function (data) { 
            	  SaleRelationPage.Search();
              },
              error: function (e, err_name, err_text) {
                  alert(err_text);
              }
          });
		   
		 
		 
	 }	,
      'SearchSalePrelevel':function(){
    	  
    	  
    	  
			var sale_id = $('#sale_id').val();
			var prelevel_size= $('#search_result_prelevel tbody tr td ').length;
			if(parseInt(prelevel_size) >1){
				
				alert("请先删除原来的上级,然后再添加新上级!");
				return;
			}
			
			layer.open({
		        type: 2,
		        title: '选择业务员',
		        fix: true,
		        shadeClose: true,
		        maxmin: true,
		        area: ['1000px', '500px'],
		        content: '/sale/navToAddPrelevelSale?sale_id='+sale_id,
		        end: function(){
		            layer.tips('完成选择', '#photosDemo', {tips: 1})
		        }
		    });
		},	
		
		'SearchSaleSublevel':function(){
			var sale_id = $('#sale_id').val();
			
			layer.open({
		        type: 2,
		        title: '选择业务员',
		        fix: true,
		        shadeClose: true,
		        maxmin: true,
		        area: ['1000px', '500px'],
		        content: '/sale/navToAddSublevelSale?sale_id='+sale_id,
		        end: function(){
		            layer.tips('完成选择', '#photosDemo', {tips: 1})
		        }
		    });
		},	
		
   
		'Search': function () {
			
			var sale_id= $('#sale_id').val();
			if(sale_id==''|| sale_id==null){
				alert("请输入业务员编号!");
				return;
			}
			
            var condition = $("#basic_validate").serialize();
          

            $.ajax({
                type: 'post',
                url: '/AjaxService/center/sale/querysalepreandsublevel',
                data: condition,
                dataType: 'json',
                async: false,
                success: function (data) {
                	SaleRelationPage.BindTableForPrelevel(data.Result);
                	SaleRelationPage.BindTableForSublevel(data.Result);
                	


                },
                error: function (e, err_name, err_text) {
                    alert(err_text);
                }
            });

        },
        
		'BindTableForPrelevel': function(result){
			lst_sale = result.datas1;
			self_sale_id=result.itself;
			if(lst_sale.length == 0){				
				$('#search_result_prelevel tbody').html('');
				var msg ="<tr><td colspan='14' style='text-align:center'>暂无数据!</td></tr>";				
				$('#search_result_prelevel tbody').append(msg);	
				return;							
			};
			var html = '';
			var flag = $('#is_Business').val();
			
			for(var index in lst_sale){
				var sale_en = lst_sale[index];
				var tr_html = "<tr>";
				tr_html += "<td>" + sale_en.sale_id+"</td>";
				tr_html += "<td>" + sale_en.sale_name+"</td>";
				tr_html += "<td>" + sale_en.sale_real_name+"</td>";
				/* tr_html += "<td>" + sale_en.area_name+"</td>"; */
				/* tr_html += "<td>" + sale_en.province_name+"</td>";
				tr_html += "<td>" + sale_en.city_name+"</td>";
				tr_html += "<td>" + sale_en.district_name+"</td>"; */
				tr_html += "<td>" + sale_en.sale_mobile + "</td>"
				tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.department) + "</td>"
				/* tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.occupation) + "</td>"
				tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.resp_area) + "</td>" */
				tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.email_address) + "</td>"
			/* 	tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.sale_address) + "</td>" */
				if(parseInt(flag) == 0){
					tr_html += "<td>"+"<a id='delrelationship' href='#' onclick=SaleRelationPage.delRelationship('"+self_sale_id+"');>刪除上级</a>"+		
					"</td>";	
				}else{
					tr_html += "<td></td>"
				}
				
				
				tr_html += "</tr>";
				html += tr_html;
			} 
			$('#search_result_prelevel tbody').html('');
			$('#search_result_prelevel tbody').append(html);
			

		},
		'BindTableForSublevel': function(result){
			lst_sale = result.datas2;
			if(lst_sale.length == 0){				
				$('#search_result_sublevel tbody').html('');
				var msg ="<tr><td colspan='14' style='text-align:center'>暂无数据!</td></tr>";				
				$('#search_result_sublevel tbody').append(msg);	
				return;							
			};
			var html = '';
			var flag = $('#is_Business').val();
			
			for(var index in lst_sale){
				var sale_en = lst_sale[index];
				var tr_html = "<tr>";
				tr_html += "<td>" + sale_en.sale_id+"</td>";
				tr_html += "<td>" + sale_en.sale_name+"</td>";
				tr_html += "<td>" + sale_en.sale_real_name+"</td>";
				/* tr_html += "<td>" + sale_en.area_name+"</td>"; */
				/* tr_html += "<td>" + sale_en.province_name+"</td>";
				tr_html += "<td>" + sale_en.city_name+"</td>";
				tr_html += "<td>" + sale_en.district_name+"</td>"; */
				tr_html += "<td>" + sale_en.sale_mobile + "</td>"
				tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.department) + "</td>"
				/* tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.occupation) + "</td>"
				tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.resp_area) + "</td>" */
				tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.email_address) + "</td>"
				/* tr_html += "<td>" + SaleRelationPage.ConvertStrNull(sale_en.sale_address) + "</td>" */
				if(parseInt(flag) == 0){
					tr_html += "<td>"+"<a id='delrelationship' href='#' onclick=SaleRelationPage.delRelationship('"+sale_en.sale_id+"');>刪除下级</a>"+		
					"</td>";	
				}else{
					tr_html += "<td></td>"
				}
				
				
				tr_html += "</tr>";
				html += tr_html;
			} 
			$('#search_result_sublevel tbody').html('');
			$('#search_result_sublevel tbody').append(html);
			

		},
		'reclick':function(){
			
			$('#btn_sale_search').trigger('click');
		},

		'ConvertStrNull':function(obj){
			
			if(obj == null){
				return '';
			}
			else{
				return obj;
			}
		},
		'ConvertSignStatus':function(obj){
			if(obj == '1'){
				return '待签约';
			}else if(obj =='2'){
				return '经销商待盖章';
			}else if(obj =='3'){
				return '已签约';
			}
		} ,		
		'ConvertStatus':function(obj){
			if(obj == '1'){
				return '待审核';
			}else if(obj =='2'){
				return '已审核';
			}else if(obj =='3'){
				return '审核拒绝';
			}
		} ,		
		
		'ConvertDealerType':function(obj){
			if(obj == '1'){
				return '一般纳税人';
			}else if(obj =='2'){
				return '个体工商户';
			}
		} ,		
		
};


$(document).ready(function(){
	SaleRelationPage.BindEvent();
	//$('#btn_sale_search').trigger('click');
});


</script>
</block>